﻿using System;
using System.IO;
using System.Linq;
using HCD.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace HCD.Web.Startup
{
    /// <summary>
    ///
    /// </summary>
    public class Program
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {                      
            var host = new WebHostBuilder()
                    .UseUrls(args)
                    .UseKestrel()
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseIISIntegration()
                    .UseStartup<Startup>()
                    .Build();
            try
            {
                var dbFactory = new EnterpriseDbContextFactory();
                var db = dbFactory.CreateDbContext(new string[] { Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") });
                Console.WriteLine("取得数据库实例");
                //更新数据库
                if (db.Database.GetPendingMigrations().Any())
                {
                    db.Database.Migrate();
                    Console.WriteLine("迁移已完成");
                }
                else
                {
                    Console.WriteLine("没有需要迁移的项目");
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("迁移失败");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
            host.Run();
        }
    }
}